home *** CD-ROM | disk | FTP | other *** search
/ 500 MB Nyheder Direkte fra Internet 2 / 500 MB nyheder direkte fra internet CD 2.iso / start / data / text / subs.txt < prev    next >
Text File  |  1994-09-21  |  27KB  |  724 lines

  1.  
  2.        SUBROUTINES
  3.   Here's a simple program:
  4. 10 PRINT "THE"
  5. 20 GO TO 1000
  6. 30 PRINT "DEAD"
  7. 40 END
  8.  
  9. 1000 PRINT "BIRD"
  10. 1010 PRINT "IS"
  11. 1020 GO TO 30
  12.   It makes the computer print:
  13. THE
  14. BIRD
  15. IS
  16. DEAD
  17.   The program consists of two parts. The main part (lines 10-40) 
  18. is called the main routine; the bottom part (lines 1000-1020) is 
  19. called the subroutine. The bottom line of the main routine says 
  20. END.
  21.   Line 20, which is in the main routine, makes the computer skip 
  22. to the subroutine. Line 1020, in the subroutine, makes the 
  23. computer return to the main routine.
  24.   To make the program more elegant, change lines 20 and 1020:
  25. 10 PRINT "THE"
  26. 20 GOSUB 1000
  27. 30 PRINT "DEAD"
  28. 40 END
  29.  
  30. 1000 PRINT "BIRD"
  31. 1010 PRINT "IS"
  32. 1020 RETURN
  33. The new line 20 says: GO to the SUBroutine that begins at line 
  34. 1000. The new line 1020 says: RETURN to the main routine, where 
  35. you left off. Like the old program, the new program prints:
  36. THE
  37. BIRD
  38. IS
  39. DEAD
  40.   GOSUB is like GO TO. The GOSUB 1000 means ``GO TO line 1000, 
  41. and remember where you came from''; so the computer goes to line 
  42. 1000, while remembering that it came from line 20. In line 1020, 
  43. the RETURN means ``RETURN to where you came from''; so the 
  44. computer returns to line 20, and then proceeds from line 20 to 
  45. line 30, which prints the word ``DEAD''.
  46.                                              Bottom lines
  47.                              The bottom line of a main routine 
  48. should say END. The bottom line of a subroutine should say 
  49. RETURN.
  50.  
  51.                                              Yankee Doodle
  52.                              This program prints the original 
  53. version of Yankee Doodle:
  54. Print the first verse:                       10 PRINT "FATHER AND 
  55. I WENT DOWN TO CAMP"
  56.                                              20 PRINT "ALONG WITH 
  57. CAPTAIN GOODING,"
  58.                                              30 PRINT "AND THERE 
  59. WE SAW THE MEN AND BOYS"
  60.                                              40 PRINT "AS THICK 
  61. AS HASTY PUDDING."
  62.  
  63. Do the chorus, and then come back:           50 GOSUB 1000
  64.  
  65. Print the second verse:                      60 PRINT "AND THERE 
  66. WAS CAPTAIN WASHINGTON"
  67.                                              70 PRINT "UPON A 
  68. SLAPPING STALLION,"
  69.                                              80 PRINT "A-GIVING 
  70. ORDERS TO HIS MEN;"
  71.                                              90 PRINT "I GUESS 
  72. THERE WERE A MILLION!"
  73.  
  74. Do the chorus, and then come back:           100 GOSUB 1000
  75.  
  76. Print the third verse:                       110 PRINT "THE 
  77. FLAMING RIBBONS IN HIS HAT,"
  78.                                              120 PRINT "THEY 
  79. LOOKED SO TARNAL FINE, AH,"
  80.                                              130 PRINT "I WANTED 
  81. POCKILY TO GET"
  82.                                              140 PRINT "TO GIVE 
  83. TO MY JEMIMAH."
  84.  
  85. Do the chorus, and then come back:           150 GOSUB 1000
  86.  
  87. That's the end of the song:                  160 END
  88.  
  89. Here's the chorus:                           1000 PRINT
  90.                                              1010 PRINT "YANKEE 
  91. DOODLE, KEEP IT UP."
  92.                                              1020 PRINT "YANKEE 
  93. DOODLE DANDY,"
  94.                                              1030 PRINT "MIND THE 
  95. MUSIC AND THE STEP,"
  96.                                              1040 PRINT "AND WITH 
  97. THE GIRLS BE HANDY."
  98.                                              1050 PRINT
  99.                                              1060 RETURN
  100.                              Lines 10-40 print the first verse. 
  101. Lines 60-90 print the second. Lines 110-140 print the third.
  102.                              At the end of each verse, the 
  103. computer is told to ``GOSUB 1000'', which means go to line 1000, 
  104. print the chorus (lines 1000-1050), and then RETURN to where it 
  105. left off.
  106.                              Lines 1000-1060, which print the 
  107. chorus, are the subroutine. The verses (lines 10-160) are the 
  108. main routine.
  109.                              Lines 1000 and 1050 make the 
  110. computer print a blank line at the beginning and end of the 
  111. chorus.
  112.                              The bottom line of the main routine 
  113. is END. The bottom line of the subroutine is RETURN.
  114.                              If you accidentally omit line 160 
  115. (which says END), the computer will proceed to do the subroutine 
  116. an extra time, and then won't know where to RETURN. It will 
  117. gripe, by saying:
  118. RETURN BEFORE GOSUB
  119. To avoid such gripes, the last line of a main routine should be 
  120. END.
  121.                     Love poem
  122.   This program prints a love poem:
  123. 10 PRINT "THE MOST BEAUTIFUL THING"
  124. 20 PRINT "IN THE WHOLE WIDE WORLD"
  125. 30 PRINT "IS..."
  126. 40 PRINT "LOVE!"
  127. 50 PRINT "THE OPPOSITE OF HATE IS"
  128. 60 PRINT "LOVE!"
  129. 70 PRINT "THE OPPOSITE OF WAR IS"
  130. 80 PRINT "LOVE!"
  131. 90 PRINT "THE OPPOSITE OF DESPAIR IS"
  132. 100 PRINT "LOVE!"
  133. 110 PRINT "AND WHEN I LOOK AT YOU,"
  134. 120 PRINT "I FEEL LOTS OF"
  135. 130 PRINT "LOVE!"
  136.   In that program, lines 40, 60, 80, 100, and 130 print the word 
  137. LOVE. Let's make those lines print the word LOVE larger, like 
  138. this:
  139. *               *       *       *     * * * * *
  140. *             *   *      *     *      *
  141. *           *       *     *   *       * * *
  142. *             *   *        * *        *
  143. * * * * *       *           *         * * * * *
  144.   Here's how:
  145.           10 PRINT "THE MOST BEAUTIFUL THING"
  146.           20 PRINT "IN THE WHOLE WIDE WORLD"
  147.           30 PRINT "IS..."
  148.           40 GOSUB 1000
  149.           50 PRINT "THE OPPOSITE OF HATE IS"
  150.           60 GOSUB 1000
  151.           70 PRINT "THE OPPOSITE OF WAR IS"
  152.           80 GOSUB 1000
  153.           90 PRINT "THE OPPOSITE OF DESPAIR IS"
  154.           100 GOSUB 1000
  155.           110 PRINT "AND WHEN I LOOK AT YOU,"
  156.           120 PRINT "I FEEL LOTS OF"
  157.           130 GOSUB 1000
  158.           140 END       
  159.  
  160. Lines 1000-10701000 PRINT "*               *       *       *     
  161. * * * * *"
  162. are the subroutine1010 PRINT "*             *   *      *     *      
  163. *"        
  164.           1020 PRINT "*           *       *     *   *       * * 
  165. *"    
  166.           1030 PRINT "*             *   *        * *        *"        
  167.           1040 PRINT "* * * * *       *           *         * * * 
  168. * *"
  169.           1050 PRINT                                                  
  170.           1060 PRINT                                                  
  171.           1070 RETURN                                                 
  172. In that new version, lines 40, 60, 80, 100, and 130 say GOSUB 
  173. 1000 instead of PRINT ``LOVE!''. The GOSUB 1000 means: do the 
  174. subroutine that begins at line 1000. The subroutine prints the 
  175. word LOVE in large letters.
  176.  
  177.         Old lady
  178.   Here are the lyrics to a famous song:
  179. There was an old lady who swallowed a fly,
  180. But I don't know why she swallowed the fly.
  181. Perhaps she'll die.
  182.  
  183. There was an old lady who swallowed a spider
  184. That wiggled and jiggled and tickled inside her.
  185. She swallowed the spider to catch the fly,
  186. But I don't know why she swallowed the fly.
  187. Perhaps she'll die.
  188.  
  189. There was an old lady who swallowed a bird.
  190. Oh, how absurd! To swallow a bird!
  191. She swallowed the bird to catch the spider
  192. That wiggled and jiggled and tickled inside her.
  193. She swallowed the spider to catch the fly,
  194. But I don't know why she swallowed the fly.
  195. Perhaps she'll die.
  196.  
  197. There was an old lady who swallowed a cat.
  198. Imagine that! To swallow a cat!
  199. She swallowed the cat to catch the bird.
  200. She swallowed the bird to catch the spider
  201. That wiggled and jiggled and tickled inside her.
  202. She swallowed the spider to catch the fly,
  203. But I don't know why she swallowed the fly.
  204. Perhaps she'll die.
  205.  
  206. There was an old lady who swallowed a dog.
  207. I swear on this log! She swallowed that dog!
  208. She swallowed the dog to catch the cat.
  209. She swallowed the cat to catch the bird.
  210. She swallowed the bird to catch the spider
  211. That wiggled and jiggled and tickled inside her.
  212. She swallowed the spider to catch the fly,
  213. But I don't know why she swallowed the fly.
  214. Perhaps she'll die.
  215.  
  216. There was an old lady who swallowed a goat.
  217. Including its coat, she swallowed that goat!
  218. She swallowed the goat to catch the dog.
  219. She swallowed the dog to catch the cat.
  220. She swallowed the cat to catch the bird.
  221. She swallowed the bird to catch the spider
  222. That wiggled and jiggled and tickled inside her.
  223. She swallowed the spider to catch the fly,
  224. But I don't know why she swallowed the fly.
  225. Perhaps she'll die.
  226.  
  227. There was an old lady who swallowed a cow.
  228. I don't know how she swallowed that cow!
  229. She swallowed the cow to catch the goat.
  230. She swallowed the goat to catch the dog.
  231. She swallowed the dog to catch the cat.
  232. She swallowed the cat to catch the bird.
  233. She swallowed the bird to catch the spider
  234. That wiggled and jiggled and tickled inside her.
  235. She swallowed the spider to catch the fly,
  236. But I don't know why she swallowed the fly.
  237. Perhaps she'll die.
  238.  
  239. There was an old lady who swallowed a horse,
  240. And she died, of course!
  241.  
  242.                              This program prints the song:
  243. 10 A$="THERE WAS AN OLD LADY WHO SWALLOWED A "
  244. 20 PRINT A$;"FLY.": GOSUB 1070
  245. 30 PRINT A$;"SPIDER": GOSUB 1050
  246. 40 PRINT A$;"BIRD.": PRINT "OH, HOW ABSURD! TO SWALLOW A BIRD!": 
  247. GOSUB 1040
  248. 50 PRINT A$;"CAT.": PRINT "IMAGINE THAT! TO SWALLOW A CAT!": 
  249. GOSUB 1030
  250. 60 PRINT A$;"DOG.": PRINT "I SWEAR ON THIS LOG! SHE SWALLOWED 
  251. THAT DOG!": GOSUB 
  252. 1020
  253. 70 PRINT A$;"GOAT.": PRINT "INCLUDING ITS COAT, SHE SWALLOWED 
  254. THAT GOAT!": GOSUB
  255.  1010
  256. 80 PRINT A$;"COW.": PRINT "I DON'T KNOW HOW SHE SWALLOWED THAT 
  257. COW!": GOSUB 1000
  258. 90 PRINT A$;"HORSE,": PRINT "AND SHE DIED, OF COURSE!"
  259. 100 END
  260.  
  261. 1000 PRINT "SHE SWALLOWED THE COW TO CATCH THE GOAT."
  262. 1010 PRINT "SHE SWALLOWED THE GOAT TO CATCH THE DOG."
  263. 1020 PRINT "SHE SWALLOWED THE DOG TO CATCH THE CAT."
  264. 1030 PRINT "SHE SWALLOWED THE CAT TO CATCH THE BIRD."
  265. 1040 PRINT "SHE SWALLOWED THE BIRD TO CATCH THE SPIDER"
  266. 1050 PRINT "THAT WIGGLED AND JIGGLED AND TICKLED INSIDE HER."
  267. 1060 PRINT "SHE SWALLOWED THE SPIDER TO CATCH THE FLY,"
  268. 1070 PRINT "BUT I DON'T KNOW WHY SHE SWALLOWED THE FLY."
  269. 1080 PRINT "PERHAPS SHE'LL DIE."
  270. 1090 PRINT
  271. 1100 RETURN
  272.                              Lines 1000-1100 are a subroutine. 
  273. That subroutine tells the whole story.
  274.                              In line 20, the GOSUB says to do 
  275. just part of the subroutine ___ the part that begins at line 
  276. 1070. Line 30 says to do more of the subroutine ___ by beginning 
  277. at line 1050. Lines 40, 50, 60, and 70 do progressively larger 
  278. chunks of the subroutine. Line 80 is the climax: it says to do 
  279. all of the subroutine.
  280.                              Line 90 provides a humorous ending.
  281.  
  282.                                                 Nesting
  283.                              One subroutine can lead to another:
  284. Main routine:                        10 PRINT "MY"
  285.                                      20 GOSUB 1000
  286.                                      30 PRINT "NOSE"
  287.                                      40 END
  288.  
  289. Subroutine 1000:                     1000 PRINT "TOE"
  290.                                      1010 PRINT "IS"
  291.                                      1020 GOSUB 2000
  292.                                      1030 PRINT "YOUR"
  293.                                      1040 RETURN
  294.  
  295. Subroutine 2000:                     2000 PRINT "STUCK"
  296.                                      2010 PRINT "IN"
  297.                                      2020 RETURN
  298.                              The program consists of three 
  299. routines. The main routine ends with END; each subroutine ends 
  300. with RETURN.
  301.                              The main routine consists of lines 
  302. 10, 20, 30, and 40. Let's see what those lines do.
  303.                              Line 10 prints MY.
  304.                              Line 20 makes the computer do 
  305. subroutine 1000, which prints TOE, prints IS, does subroutine 
  306. 2000 (STUCK and IN), and prints YOUR.
  307.                              Line 30 prints NOSE.
  308.                              Line 40 ends.
  309.                              So altogether, the computer prints:
  310. MY
  311. TOE
  312. IS
  313. STUCK
  314. IN
  315. YOUR
  316. NOSE
  317.                              In that example, subroutine 2000 is 
  318. nested in subroutine 1000.
  319.  
  320.              SUBSCRIPTS
  321.   Instead of being a single string, X$ can be a whole list of 
  322. strings, like this:
  323.       "HATE"
  324.       "LOVE"
  325.       "KILL"
  326. X$=   "KISS"
  327.       "WAR"
  328.       "PEACE"
  329.       "WHY"
  330. Here's how to make X$ be that list of strings. . . . 
  331.   Begin your program by saying:
  332. 10 DIM X$(7)
  333. That says X$ will be a list of 7 strings. DIM means dimension; 
  334. the line says the dimension of X$ is 7.
  335.   Next, tell the computer what strings are in X$. Type these 
  336. lines:
  337. 20 X$(1)="HATE"
  338. 30 X$(2)="LOVE"
  339. 40 X$(3)="KILL"
  340. 50 X$(4)="KISS"
  341. 60 X$(5)="WAR"
  342. 70 X$(6)="PEACE"
  343. 80 X$(7)="WHY"
  344. Line 20 says X$'s first string is HATE. Line 30 says X$'s second 
  345. string is LOVE. The remaining lines define the other strings in 
  346. X$.
  347.   If you'd like the computer to print all those strings, type 
  348. this:
  349. 90 FOR I = 1 TO 7: PRINT X$(I): NEXT
  350. That means: print all the strings in X$. The computer will print:
  351. HATE
  352. LOVE
  353. KILL
  354. KISS
  355. WAR
  356. PEACE
  357. WHY
  358.   In that program, line 20 talks about X$(1). Instead of saying 
  359. X$(1), math books say:
  360. X1
  361. The ``1'' is called a subscript. Similarly, in line 30, which 
  362. says X$(2)=``LOVE'', the number 2 is a subscript. Some 
  363. programmers pronounce line 30 like this: ``X string, subscripted 
  364. by 2, is LOVE''. Some programmers simply say: ``X string 2 is 
  365. LOVE''.
  366.   In that program, X$ is called an array (or matrix). Definition: 
  367. an array (or matrix) is a variable that has subscripts.
  368.  
  369.           Subscripted DATA
  370.   That program said X$(1) is HATE, and X$(2) is LOVE, and so on. 
  371. This program does the same thing, more briefly:
  372. 10 DIM X$(7)
  373. 20 DATA HATE,LOVE,KILL,KISS,WAR,PEACE,WHY
  374. 30 FOR I=1 TO 7: READ X$(I): NEXT
  375. 40 FOR I=1 TO 7: PRINT X$(I): NEXT
  376. Line 10 says X$ will be a list of 7 strings. Line 20 contains a 
  377. list of 7 strings. Line 30 makes the computer READ those strings 
  378. and call them X$. Line 40 makes the computer print them.
  379.   In that program, the first three lines say:
  380. DIM
  381. DATA
  382. FOR I
  383. Most practical programs begin with those three lines.
  384.                                          Let's lengthen the 
  385. program, so that the computer prints all this:
  386. HATE
  387. LOVE
  388. KILL
  389. KISS
  390. WAR
  391. PEACE
  392. WHY
  393.  
  394. WHY HATE
  395. WHY LOVE
  396. WHY KILL
  397. WHY KISS
  398. WHY WAR
  399. WHY PEACE
  400. WHY WHY
  401. That consists of two verses. The second verse resembles the first 
  402. verse, except that each line of the second verse begins with WHY.
  403.                                          To make the computer 
  404. print all that, just add these lines to the program:
  405. 50 PRINT
  406. 60 FOR I = 1 TO 7: PRINT "WHY ";X$(I): NEXT
  407. Line 50 leaves a blank line between the first verse and the 
  408. second verse. Line 60 prints the second verse. Line 60 resembles 
  409. line 40 (which printed the first verse), except that line 60 
  410. prints ``WHY '' before each X$(I).
  411.                                          Let's add a third verse, 
  412. which prints the words in reverse order:
  413. WHY
  414. PEACE
  415. WAR
  416. KISS
  417. KILL
  418. LOVE
  419. HATE
  420. Before printing that third verse, print a blank line:
  421. 70 PRINT
  422. Then print the verse itself. To print the verse, you must print 
  423. X$(7), then print X$(6), then print X$(5), etc. To do that, you 
  424. could say:
  425. 80 PRINT X$(7)
  426. 90 PRINT X$(6)
  427. 100 PRINT X$(5)
  428. etc.
  429. But this way is shorter:
  430. 80 FOR I = 7 TO 1 STEP -1: PRINT X$(I): NEXT
  431.  
  432.                                                   Numeric arrays
  433.                                          Let's make Y be this 
  434. list of six numbers: 100, 26, 94, 201, 8.3, and -7. To begin, 
  435. tell the computer that Y will consist of six numbers:
  436. 10 DIM Y(6)
  437. Next, tell the computer what the six numbers are:
  438. 20 DATA 100,26,94,201,8.3,-7
  439. Make the computer READ all that data:
  440. 30 FOR I = 1 TO 6: READ Y(I): NEXT
  441.                                          To make the computer 
  442. PRINT all that data, type this:
  443. 40 FOR I = 1 TO 6: PRINT Y(I): NEXT
  444. If you want the computer to add those 6 numbers together and 
  445. print their sum, say:
  446. 50 PRINT Y(1)+Y(2)+Y(3)+Y(4)+Y(5)+Y(6)
  447.                  Strange example
  448.   Getting tired of X and Y? Then pick another letter! For 
  449. example, you can play with Z:
  450. Silly, useless programWhat the program means
  451. 10 DIM Z(5)           Z will be a list of 5 numbers.
  452. 20 FOR I = 2 TO 5
  453. 30     Z(I)=I*100     Z(2)=200. Z(3)=300. Z(4)=400. Z(5)=500.
  454. 40 NEXT
  455. 50 Z(1)=Z(2)-3        Z(1) is 200-3, so Z(1) is 197.
  456. 60 Z(3)=Z(1)-2        Z(3) changes to 197-2, which is 195.
  457. 70 FOR I = 1 TO 5: PRINT Z(I): NEXTPrint Z(1), Z(2), Z(3), Z(4), 
  458. and Z(5).
  459.   Line 70 prints:
  460.  197
  461.  200
  462.  195
  463.  400
  464.  500
  465.  
  466.              Problems and solutions
  467.   Suppose you want to analyze 50 numbers. Begin your program by 
  468. saying:
  469. 10 DIM X(50)
  470. Then type the 50 numbers, as data, like this:
  471. 20 DATA etc.
  472. 30 DATA etc.
  473. 40 DATA etc.
  474. Tell the computer to READ the data:
  475. 100 FOR I=1 TO 50: READ X(I): NEXT
  476.   After line 100, do one of the following, depending on which 
  477. problem you want to solve. . . . 
  478.   Print all the values of X Solution:
  479. 110 FOR I = 1 TO 50: PRINT X(I): NEXT
  480.   Print all the values of X, in reverse order Solution:
  481. 110 FOR I = 50 TO 1 STEP -1: PRINT X(I): NEXT
  482.   Print the sum of all the values of X In other words, print 
  483. X(1)+X(2)+X(3)+ . . . +X(50). Solution: start the sum at 0 ___ 
  484. 110 S=0
  485. and then increase the sum, by adding each X(I) to it:
  486. 120 FOR I = 1 TO 50: S=S+X(I): NEXT
  487. Finally, print the sum:
  488. 130 PRINT "THE SUM OF ALL THE NUMBERS IS";S
  489.   Find the average of X In other words, find the average of the 
  490. 50 numbers. Solution: begin by finding the sum ___ 
  491. 110 S=0
  492. 120 FOR I = 1 TO 50: S=S+X(I): NEXT
  493. and then divide the sum by 50:
  494. 130 PRINT "THE AVERAGE IS";S/50
  495.   Find out whether any of the values of X is 79.4 In other words, 
  496. find out whether 79.4 is a number in the list. Solution: if X(I) 
  497. is 79.4, print ``YES'' ___ 
  498. 110 FOR I = 1 TO 50
  499. 120    IF X(I)=79.4 THEN PRINT "YES, 79.4 IS IN THE LIST": END
  500. 130 NEXT
  501. otherwise, print ``NO'':
  502. 140 PRINT "NO, 79.4 IS NOT IN THE LIST"
  503.  
  504.  
  505.   In X's list, count how often 79.4 appears Solution: start the 
  506. counter at zero ___ 
  507. 110 C=0
  508. and increase the counter each time you see the number 79.4:
  509. 120 FOR I = 1 TO 50
  510. 130    IF X(I)=79.4 THEN C=C+1
  511. 140 NEXT
  512. Finally, print the counter:
  513. 150 PRINT "THE NUMBER 79.4 APPEARS";C;"TIMES"
  514.   Print all the values of X that are negative In other words, 
  515. print all the numbers that have minus signs. Solution: begin by 
  516. announcing your purpose ___ 
  517. 110 PRINT "HERE ARE THE VALUES THAT ARE NEGATIVE:"
  518. and then print the values that are negative; in other words, 
  519. print each X(I) that's less than 0:
  520. 120 FOR I = 1 TO 50
  521. 130    IF X(I)<0 THEN PRINT X(I)
  522. 140 NEXT
  523.   Print all the values of X that are ``above average'' Solution: 
  524. find the average, and call it A, like this ___ 
  525. 110 S=0
  526. 120 FOR I = 1 TO 50: S=S+X(I): NEXT
  527. 130 A=S/50
  528. then announce your purpose:
  529. 140 PRINT "THE FOLLOWING VALUES ARE ABOVE AVERAGE:"
  530. Finally, print the values that are above average; in other words, 
  531. print each X(I) that's greater than A:
  532. 150 FOR I = 1 TO 50
  533. 160    IF X(I)>A THEN PRINT X(I)
  534. 170 NEXT
  535.   Find the biggest value of X In other words, find which of the 
  536. 50 numbers is the biggest. Solution: let B stand for the biggest 
  537. number. Begin by tentatively setting B equal to the first number 
  538. ___ 
  539. 110 B=X(1)
  540. but if another number is bigger than that B, change B:
  541. 120 FOR I = 2 TO 50
  542. 130    IF X(I)>B THEN B=X(I)
  543. 140 NEXT
  544. Afterwards, print B:
  545. 150 PRINT "THE BIGGEST NUMBER IN THE LIST IS";B
  546.   Find the smallest value of X In other words, find which of the 
  547. 50 numbers is the smallest. Solution: let S stand for the 
  548. smallest number. Begin by tentatively setting S equal to the 
  549. first number ___ 
  550. 110 S=X(1)
  551. but if another number is smaller than S, change S:
  552. 120 FOR I = 2 TO 50
  553. 130    IF X(I)<S THEN S=X(I)
  554. 140 NEXT
  555. Afterwards, print S:
  556. 150 PRINT "THE SMALLEST NUMBER IN THE LIST IS";S
  557.   Check whether X's list is in strictly increasing order In other 
  558. words, find out whether the following statement is true: X(1) is 
  559. a smaller number than X(2), which is a smaller number than X(3), 
  560. which is a smaller number than X(4), etc. Solution: if X(I) is 
  561. not smaller than X(I+1), print ``NO'' ___ 
  562. 110 FOR I = 1 TO 49
  563. 120    IF X(I)>=X(I+1) THEN PRINT "NO, THE LIST IS NOT IN 
  564. STRICTLY INCREASING OR
  565. DER": END
  566. 130 NEXT
  567. otherwise, print ``YES'':
  568. 140 PRINT "YES, THE LIST IS IN STRICTLY INCREASING ORDER"
  569.  
  570.  
  571.   Test yourself: look at those problems again, and see whether 
  572. you can figure out the solutions without peeking at the answers.
  573.  
  574.                  Multiple arrays
  575.   Suppose your program involves three lists. Suppose the first 
  576. list is called A$ and consists of 18 strings; the second list is 
  577. called B and consists of 57 numbers; and the third list is called 
  578. C$ and consists of just 3 strings. To say all that, begin your 
  579. program with this statement:
  580. 10 DIM A$(18),B(57),C$(3)
  581.  
  582.                 Double subscripts
  583.   You can make X be a table of strings, like this:
  584.      "DOG"     "CAT"     "MOUSE"
  585. X$=
  586.      "HOTDOG"  "CATSUP"  "MOUSETARD"
  587. Here's how to make X$ be that table. . . . 
  588.   Begin by saying:
  589. 10 DIM X$(2,3)
  590. That says X$ will be a table having 2 rows and 3 columns.
  591.   Then tell the computer what strings are in X$. Type these 
  592. lines:
  593. 20 X$(1,1)="DOG"
  594. 30 X$(1,2)="CAT"
  595. 40 X$(1,3)="MOUSE"
  596. 50 X$(2,1)="HOTDOG"
  597. 60 X$(2,2)="CATSUP"
  598. 70 X$(2,3)="MOUSETARD"
  599. Line 20 says: the string in X$'s first row and first column is 
  600. DOG. Line 30 says the string in X$'s first row and second column 
  601. is CAT. The remaining lines define the other strings in X$.
  602.   If you'd like the computer to print all those strings, type 
  603. this:
  604. 80 FOR I = 1 TO 2: FOR J = 1 TO 3: PRINT X$(I,J),: NEXT: PRINT: 
  605. NEXT
  606. That means: print all the strings in X$. The computer will print:
  607. DOG             CAT             MOUSE
  608. HOTDOG          CATSUP          MOUSETARD
  609.   In that program, X$ is called a table or two-dimensional array 
  610. or doubly subscripted array.
  611.  
  612.               Multiplication table
  613.   This program prints a multiplication table:
  614. 10 DIM X(10,4)
  615. 20 FOR I = 1 TO 10: FOR J = 1 TO 4: X(I,J)=I*J: NEXT: NEXT
  616. 30 FOR I = 1 TO 10: FOR J = 1 TO 4: PRINT X(I,J),: NEXT: PRINT: 
  617. NEXT
  618.   Line 10 says X will be a table having 10 rows and 4 columns.
  619.   The middle of line 20 says X(I,J)=I*J. That means the number in 
  620. row I and column J is I*J. For example, the number in row 3 and 
  621. column 4 is 12.
  622.   The beginning of line 20 says ``FOR I = 1 TO 10: FOR J = 1 TO 
  623. 4'', so that X(I,J)=I*J for every I and J, so every entry in the 
  624. table is defined by multiplication.
  625.   Line 30 prints the whole table:
  626. 1               2               3               4
  627. 2               4               6               8
  628. 3               6               9               12
  629. 4               8               12              16
  630. 5               10              15              20
  631. 6               12              18              24
  632. 7               14              21              28
  633. 8               16              24              32
  634. 9               18              27              36
  635. 10              20              30              40
  636.   Instead of multiplication, you can have addition, subtraction, 
  637. or division: just change line 20.
  638.   Most programmers follow this tradition: the row's number is 
  639. called I, and the column's number is called J. Line 20 obeys that 
  640. tradition. Notice I comes before J in the alphabet; I comes 
  641. before J in X(I,J); and ``FOR I'' comes before ``FOR J''. If you 
  642. follow the I-before-J tradition, you'll make fewer errors.
  643.  
  644.      Summing a table
  645.   Suppose you want to analyze this table:
  646.   32.7    19.4    31.6    85.1
  647.   -8     402     -61       0
  648. 5106       -.2     0      -1.1
  649.   36.9      .04    1      11
  650.  777     666      55.44    2
  651.    1.99    2.99    3.99    4.99
  652.   50      40      30      20
  653.   12      21      12      21
  654.    0    1000       2     500
  655.   Since the table has 9 rows and 4 columns, begin your program by 
  656. saying:
  657. 10 DIM X(9,4)
  658. Each row of the table becomes a row of the DATA:
  659. 11 DATA 32.7, 19.4, 31.6, 85.1
  660. 12 DATA -8, 402, -61, 0
  661. 13 DATA 5106, -.2, 0, -1.1
  662. 14 DATA 36.9, .04, 1, 11
  663. 15 DATA 777, 666, 55.44, 2
  664. 16 DATA 1.99, 2.99, 3.99, 4.99
  665. 17 DATA 50, 40, 30, 20
  666. 18 DATA 12, 21, 12, 21
  667. 19 DATA 0, 1000, 2, 500
  668.  
  669. Make the computer READ the data:
  670. 20 FOR I = 1 TO 9: FOR J = 1 TO 4: READ X(I,J): NEXT: NEXT
  671. To make the computer print the table, say this:
  672. 30 FOR I = 1 TO 9: FOR J = 1 TO 4: PRINT X(I,J),: NEXT: PRINT: 
  673. NEXT
  674.                              Here are some problems, with 
  675. solutions. . . . 
  676.                              Find the sum of all the numbers in 
  677. the table Solution: start the sum at 0 ___ 
  678. 100 S=0
  679. and then increase the sum, by adding each X(I,J) to it:
  680. 110 FOR I = 1 TO 9: FOR J = 1 TO 4: S=S+X(I,J): NEXT: NEXT
  681. Finally, print the sum:
  682. 120 PRINT "THE SUM OF ALL THE NUMBERS IS";S
  683. The computer will print:
  684. THE SUM OF ALL THE NUMBERS IS 8877.84
  685.                              Find the sum of each row In other 
  686. words, make the computer print the sum of the numbers in the 
  687. first row, then the sum of the numbers in the second row, then 
  688. the sum of the numbers in the third row, etc. Solution: the 
  689. general idea is ___ 
  690. 100 FOR I = 1 TO 9
  691. 110     print the sum of row I
  692. 120 NEXT
  693. Here are the details:
  694. 110 FOR I = 1 TO 9
  695. 110     S=0
  696. 111     FOR J = 1 TO 4: S=S+X(I,J): NEXT
  697. 112     PRINT "THE SUM OF ROW";I;"IS";S
  698. 120 NEXT
  699. The computer will print:
  700. THE SUM OF ROW 1 IS 168.8
  701. THE SUM OF ROW 2 IS 333
  702. THE SUM OF ROW 3 IS 5104.7
  703. etc.
  704.                              Find the sum of each column In other 
  705. words, make the computer print the sum of the numbers in the 
  706. first column, then the sum of the numbers in the second column, 
  707. then the sum of the numbers in the third column, etc. Solution: 
  708. the general idea is ___ 
  709. 100 FOR J = 1 TO 4
  710. 110     print the sum of column J
  711. 120 NEXT
  712. Here are the details:
  713. 100 FOR J = 1 TO 4
  714. 110     S=0
  715. 111     FOR I = 1 TO 9: S=S+X(I,J): NEXT
  716. 112     PRINT "THE SUM OF COLUMN";J;"IS";S
  717. 120 NEXT
  718. The computer will print:
  719. THE SUM OF COLUMN 1 IS 6008.59
  720. THE SUM OF COLUMN 2 IS 2151.23
  721. THE SUM OF COLUMN 3 IS 75.03
  722. THE SUM OF COLUMN 4 IS 642.99
  723. In all the other examples, ``FOR I'' came before ``FOR J''; but 
  724. in this unusual example, ``FOR I'' comes after ``FOR J''.